package com.colin.datamove.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.colin.datamove.entity.CompanyModel;
import com.colin.datamove.mapper.db1.AssetCompanyDao;
import com.colin.datamove.service.MoveService;
import com.colin.datamove.util.LogUtil;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

@Service("assetCompanyService")
public class AssetCompanyServiceImpl implements MoveService {

    @Resource
    private AssetCompanyDao db1AssetCompanyDao;
    @Resource
    private com.colin.datamove.mapper.db2.AssetCompanyDao db2db1AssetCompanyDao;

    @Override
    public void moveJob(int pmc) {
        // 查询老数据库需要迁移数据
        List<CompanyModel> unMoveData = db2db1AssetCompanyDao.getUnMoveData(pmc);
        LogUtil.info("AssetCompanyService 此次待迁移数据:{}条", unMoveData.size());
        if (CollectionUtils.isNotEmpty(unMoveData)) {
            // 更新数据为已迁移
            int i = db2db1AssetCompanyDao.updateMoved(unMoveData);
            LogUtil.info("AssetCompanyService 更新待迁移数据为已迁移:{}条", i);

            // 插入新数据库
            int i1 = db1AssetCompanyDao.insertData(unMoveData);
            LogUtil.info("AssetCompanyService 新库中插入数据:{}条", i1);
        }

        // 更新新数据库中的主键关联数据，
        int i2 = db1AssetCompanyDao.updateParentId();
        LogUtil.info("AssetCompanyService 新库中更新parentId数据:{}条", i2);

        int i3 = db1AssetCompanyDao.updateRootCompanyId();
        LogUtil.info("AssetCompanyService 新库中更新rootCompanyId数据:{}条", i3);
    }

    public static void main(String[] args) {
        Map<String, Object> map = new LinkedHashMap<>();
        map.put("1", "1");
        map.put("2", null);
        System.out.println(JSONObject.toJSONString(map));
    }


}
